//
// Created by Semir on 2020/4/26.
//

#include <iostream>
#include <deque>
#include <string>
#include <map>

using namespace std;

map<string, int> table = {{"PUSH", 0}, {"TOP", 1}, {"POP", 2},
                          {"SIZE", 3}, {"CLEAR", 4}};
int group, n;
string op;

int main() {
    cin >> group;
    for (int i = 0; i < group; i++) {
        cin >> n;
        deque<int> Q;
        for (int j = 0; j < n; j++) {
            cin >> op;
            switch (table[op]) {
                case 0:
                    int tmp;
                    cin >> tmp;
                    Q.push_back(tmp);
                    break;
                case 1:
                    if (!Q.empty()) {
                        cout << Q.front() << endl;
                    } else {
                        cout << "-1" << endl;
                    }
                    break;
                case 2:
                    if (!Q.empty()) {
                        Q.pop_front();
                    } else {
                        cout << "-1" << endl;
                    }
                    break;
                case 3:
                    cout << Q.size() << endl;
                    break;
                case 4:
                    Q.clear();
                    break;
                default:
                    break;
            }
        }
    }
    return 0;
}